package csu.web.mypetstore.controller;

import csu.web.mypetstore.pojo.Account;
import csu.web.mypetstore.pojo.vo.LoginRequest;
import csu.web.mypetstore.service.AccountService;
import csu.web.mypetstore.utils.Result;
import csu.web.mypetstore.utils.ResultToJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@CrossOrigin
public class AccountController {
    @Autowired
    private AccountService accountService;


    

    @PostMapping(value = "/account")
    public Result account( @ModelAttribute Account account,@RequestPart(value = "file", required = false) MultipartFile file) {//注册功能


        accountService.register(account,file);

        return Result.success();

    }
    @GetMapping(value = "/account/{username}")
    public Result getAccount(@PathVariable("username") String username) {
        return Result.success(accountService.getAccount(username));
    }
    @PutMapping(value = "/account/{username}")
    public Result updateAccount(@PathVariable("username") String username, @RequestBody Account account) {

        accountService.updateAccount(account);
        return Result.success("更新成功");
    }
    @DeleteMapping(value = "/account/{username}")
    public Result deleteAccount(@PathVariable("username") String username) {
        accountService.deleteAccount(username);
        return Result.success("删除成功");
    }
    @PostMapping(value = "/token")
    public Result login(@RequestBody LoginRequest loginRequest) {
        Result result = accountService.login( loginRequest.getUsername(), loginRequest.getPassword());
        return result;
    }

}
